﻿
using Ch13CardLib.enums;
using Ch13CardLib.exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch13CardLib.entity
{
    public class Deck
    {
        public List<Card> CardList { get; set; }

        public Deck() {

            InitCardList();

            //Shuffle();


        }

        /**
         * 初始化牌
         */
        public void InitCardList()
        {

            CardList = new List<Card>();

            int i = 0;
            foreach (Suit oneSuit in Enum.GetValues(typeof(Suit)))
            {

                foreach (Rank oneRank in Enum.GetValues(typeof(Rank)))
                {
                    Card oneCard = new Card(i, oneSuit, oneRank);

                    CardList.Add(oneCard);
                    i++;

                }
            }

        }

        public int GetRandomNum(int min, int max)
        {
            Random random = new Random();
            int number = random.Next(min, max);
            return number;
        }


        private List<Card> ShuffleByRemoving(List<Card> originalList)
        {
            List<Card> shuffledList = new List<Card>();
            Random random = new Random();

            while (originalList.Count > 0)
            {
                int index = random.Next(originalList.Count);
                Card item = originalList[index];
                shuffledList.Add(item);
                originalList.RemoveAt(index);
            }

            return shuffledList;
        }

        private List<Card> ShuffleYate(List<Card> list)
        {
            Random random = new Random();
            int n = list.Count;
            for (int i = n - 1; i > 0; i--)
            {
                int j = random.Next(i + 1);
                Card temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }

            return list;
        }

        /**
         * 洗牌
         */
        public void Shuffle()
        {
            this.CardList = this.ShuffleYate(this.CardList);

        }

        public Card GetCard(int cardNumber)
        {
            if (cardNumber >= 0 && cardNumber < CardList.Count)
            {

                Card resultCard = CardList.ElementAt(cardNumber);
                return resultCard;
            }
            else
            {
                throw new CardOutOfRangeException(this);
            }


        }

    }
}
